package com.shaoshaohuo.app.utils;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

import org.hybridsquad.android.library.CropHandler;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 相机、相册工具
 * Created by YinSF on 16/1/23.
 */
public class CameraUtil {

    public static final int REQUEST_CAPTURE = 100;
    public static final int REQUEST_PHOTO = 101;
    public static final int REQUEST_CROP = 102;

    public static boolean saveImage(Bitmap photo, String spath) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(
                    new FileOutputStream(spath, false));
            photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    public static Uri saveImage(Intent data, String spath) {
        Bundle bundle = data.getExtras();
        if (bundle != null) {
            //spath :生成图片取个名字和路径包含类型
            Bitmap photo = (Bitmap) bundle.get("data");
            boolean isSaved = saveImage(photo, spath);
            if (isSaved) {
                return Uri.parse(spath);
            }
        }
        return null;
    }

    public static Uri buildUri(String name) {
        return Uri.fromFile(new File(Environment.getExternalStorageDirectory(), name));
    }

    /**
     * 相机
     * @param context
     */
    public static void capture(CropHandler context) {
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        context.getContext().startActivityForResult(camera, REQUEST_CAPTURE);
    }

    /**
     * 相册
     * @param context
     */
    public static void photo(CropHandler context) {
        Intent camera = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        context.getContext().startActivityForResult(camera, REQUEST_PHOTO);
    }

    public static void handleResult(CropHandler handler, int requestCode, int resultCode, Intent data) {
        if(handler != null) {
            if(requestCode == REQUEST_CAPTURE && resultCode == Activity.RESULT_OK && null != data){
                Uri picturePath = saveImage(data, buildUri(System.currentTimeMillis() + ".jpg").getPath());
                handler.onPhotoCropped(picturePath);
                return;
            }

            if(requestCode == REQUEST_PHOTO && resultCode == Activity.RESULT_OK && null != data){

                Uri selectedImage = data.getData();
                String[] filePathColumns={MediaStore.Images.Media.DATA};
                Cursor c = handler.getContext().getContentResolver().query(selectedImage, filePathColumns, null,null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePathColumns[0]);
                String picturePath= c.getString(columnIndex);
                c.close();
                handler.onPhotoCropped(Uri.parse(picturePath));
            }
        }
    }

}
